Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

EnumSet → Java EnumSet

EnumSet

Java EnumSet

EnumSets

EnumSet is a specialized set implementation designed specifically for working with enum (enumeration) values. It offers a memory-efficient and type-safe way to store a collection of distinct elements from a single enum type.

Characteristics of EnumSets

Enum-Specific: EnumSet can only hold elements of a single enum type. ⯌ Efficiency: It uses a bitset internally to represent the included enum values, leading to efficient memory usage and faster operations compared to general-purpose sets like HashSet. ⯌ Type Safety: Since it's bound to an enum type, EnumSet enforces type safety and prevents accidental addition of non-enum elements. ⯌ Thread-Safety: EnumSet is thread-safe, meaning it can be used in concurrent environments without extra synchronization.

Declaring and Initializing EnumSets

Import:
EnumSet import syntax import java.util.EnumSet;
This line imports the EnumSet class from the java.util package.
Declaration
Declaring EnumSet EnumSet<EnumType> mySet;
This declares a variable named mySet of type EnumSet. The <EnumType> placeholder specifies the enum type whose elements the set will hold.
Initialization: There are several ways to initialize an EnumSet: Empty set:
Creating empty EnumSet mySet = EnumSet.noneOf(EnumType.class);

Set of all elements from the enum:
Set of All elements mySet = EnumSet.allOf(EnumType.class);

Set of specific enum values:
Set of specific elements mySet = EnumSet.of(EnumType.VALUE1, EnumType.VALUE2);

Copying from another EnumSet:
copying from another EnumSet EnumSet<EnumType> copySet = EnumSet.copyOf(anotherSet);

Common EnumSet Methods:

allOf(EnumType.class): Creates a set containing all elements of the specified enum type. ⯌ noneOf(EnumType.class): Creates an empty EnumSet for the specified enum type. ⯌ of(EnumType1, EnumType2, ...): Creates a set containing the specified enum elements. ⯌ complementOf(EnumSet<EnumType> set): Returns a new set containing all elements of the enum type that are not present in the specified set. ⯌ contains(EnumType element): Checks if the set contains a specific enum element.

Examples for EnumSet under collections in Java - of()

Creating a Set with Specific Enum Values:
EnumSet simple example in Java import java.util.*; enum Color { RED, GREEN, BLUE, YELLOW } public class Main { public static void main(String[] args) { EnumSet<Color> primaryColors = EnumSet.of(Color.RED, Color.GREEN, Color.BLUE); for (Color color : primaryColors) { System.out.println(color); } } }

Output

RED GREEN BLUE
Explanation : This code creates an EnumSet named primaryColors containing the RED, GREEN, and BLUE values from the Color enum. It then iterates through the set and prints each color.
Creating a Set with All Enum Values:
EnumSet simple example in Java - allOf() import java.util.*; enum Size { SMALL, MEDIUM, LARGE } public class Main { public static void main(String[] args) { EnumSet<Size> allSizes = EnumSet.allOf(Size.class); for (Size size : allSizes) { System.out.println(size); } } }

Output

SMALL MEDIUM LARGE
Explanation : This code creates an EnumSet named allSizes that contains all the values (SMALL, MEDIUM, LARGE) of the Size enum using EnumSet.allOf().
Creating an Empty EnumSet:
EnumSet simple example in Java - noneOf() import java.util.*; enum Permission { READ, WRITE, EXECUTE } public class Main { public static void main(String[] args) { EnumSet<Permission> noPermissions = EnumSet.noneOf(Permission.class); // Check if the set is empty if (noPermissions.isEmpty()) { System.out.println("No permissions granted."); } } }

Output

No permissions granted.
Explanation : This code creates an empty EnumSet named noPermissions for the Permission enum using EnumSet.noneOf(). It then checks if the set is empty and prints a message if it is.
Creating an EnumSet from a Collection of Enum Values:
EnumSet simple example in Java - copyOf() import java.util.*; enum Fruit { APPLE, BANANA, ORANGE } public class Main { public static void main(String[] args) { Fruit[] favoriteFruits = { Fruit.APPLE, Fruit.ORANGE }; EnumSet<Fruit> fruitSet = EnumSet.copyOf(List.of(favoriteFruits)); for (Fruit fruit : fruitSet) { System.out.println(fruit); } } }

Output

APPLE ORANGE
Explanation : This code defines an array favoriteFruits containing some Fruit enum values. It then creates an EnumSet named fruitSet as a copy of this list using EnumSet.copyOf(). The List.of method is used to create an immutable list from the array.
Adding and Removing Elements from an EnumSet:
EnumSet simple example in Java - add() and remove() import java.util.*; enum TrafficLight { RED, YELLOW, GREEN } public class Main { public static void main(String[] args) { EnumSet<TrafficLight> light = EnumSet.of(TrafficLight.RED); light.add(TrafficLight.YELLOW); // Add YELLOW light.remove(TrafficLight.RED); // Remove RED for (TrafficLight state : light) { System.out.println(state); } } }

Output

YELLOW
Explanation : This code creates an EnumSet named light with the initial value RED. It then demonstrates adding the YELLOW value and removing the RED value using the add() and remove() methods of EnumSet. Finally, it iterates through the modified set and prints the remaining element (YELLOW). Remember, EnumSet offers a compact and efficient way to work with sets of enum values in Java applications.

Tutorials